package com.hnust.controller.subject.dialog;

import com.hnust.domain.AnswerTestObj;
import com.hnust.domain.SubjectTest;
import com.hnust.fx.FXMLController;
import com.hnust.store.SubjectStore;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

/**
 * 创建人：zhangRuWang
 * 创建时间：2020/12/13
 * 描述：
 */
@FXMLController
public class CheckSubjectController implements Initializable {

    @FXML
    public TextField text4;
    @FXML
    private WebView contentHtml;
    @FXML
    private WebView resolveHtml;
    @FXML
    private ComboBox<String> comb5;
    @FXML
    private ComboBox<String> comb3;
    @FXML
    private ComboBox<String> comb2;
    @FXML
    private RadioButton r1;
    @FXML
    private RadioButton r2;
    @FXML
    private RadioButton r3;
    @FXML
    private VBox falseVbox;
    @FXML
    private VBox trueVbox;
    @FXML
    private AnchorPane checkSubjectWindow;
    @FXML
    private ComboBox<String> comb1;

    @Autowired
    private SubjectStore subjectStore;

    private SubjectTest subjectTest;

    ObservableList<String> data1= FXCollections.observableList(new ArrayList<>());
    ObservableList<String> data2= FXCollections.observableList(new ArrayList<>());
    ObservableList<String> data3= FXCollections.observableList(new ArrayList<>());
    ObservableList<String> data5= FXCollections.observableList(new ArrayList<>());


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Screen s=Screen.getPrimary();
        Rectangle2D visualBound=s.getVisualBounds();
        checkSubjectWindow.setPrefHeight(visualBound.getHeight()*0.6);
        checkSubjectWindow.setPrefWidth(visualBound.getWidth()*0.8);
        checkSubjectWindow.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                subjectTest=subjectStore.getCheckSubject();
                initData();

            }else{
                clearData();
            }
        });
    }

    private void clearData() {
        r1.setSelected(false);r2.setSelected(false);r3.setSelected(false);
        comb1.setValue(null);comb2.setValue(null);comb3.setValue(null);
        text4.setText(null);contentHtml.getEngine().loadContent(null);resolveHtml.getEngine().loadContent(null);
        trueVbox.getChildren().clear();
        falseVbox.getChildren().clear();
    }

    public void initData(){
        //单选数据导入
        if(subjectTest.getDifficult()==3){
            r1.setSelected(true);
        }else if(subjectTest.getDifficult()==2){
            r2.setSelected(true);
        }else{
            r3.setSelected(true);
        }
        //题目类型
        data1.clear();
        data1.add(subjectTest.getType_name());
        comb1.setItems(data1);
        comb1.getSelectionModel().select(0);

        //题目课程
        data2.clear();
        data2.add(subjectTest.getCourse_name());
        comb2.setItems(data2);
        comb2.getSelectionModel().select(0);
        //题目章节
        data3.clear();
        data3.add(subjectTest.getChapter_name());
        comb3.setItems(data3);
        comb3.getSelectionModel().select(0);
        //知识点
        text4.setText(subjectTest.getKnowledgePointer());
        //课程目标
        data5.clear();
        data5.add(subjectTest.getCourseTarget());
        comb5.setItems(data5);
        comb5.getSelectionModel().select(0);
        //题目内容和解析
        contentHtml.getEngine().loadContent(subjectTest.getContent());
        resolveHtml.getEngine().loadContent(subjectTest.getResolve());
        if(subjectTest.getAnswer()!=null){
            for(AnswerTestObj a:subjectTest.getAnswer()){
                WebView t=new WebView();
                t.getEngine().loadContent(a.getContent());
                t.setMaxHeight(200);
                if(a.getRight()){
                    trueVbox.getChildren().addAll(t);
                }else{
                    falseVbox.getChildren().addAll(t);
                }
            }
        }
    }
}
